﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Domain.Query.SiteModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.SiteModule.DataTransferObject;
using Resto.CostControl.Core.Enums;
using Resto.CostControl.Core.Domain.Model;
using Enterprise.Framework.Core.Exceptions;

namespace Resto.CostControl.Core.Facade.SiteModule.FacadeObject
{
    /// <summary>
    /// 站点收入FO
    /// </summary>
    public class SiteIncomeFO : IApplicationFacadeObject
    {
        #region 私有属性

        private SiteIncomeDQO _SiteIncomeDQO;
        private SiteIncomeDQO SiteIncomeDQO
        {
            get
            {
                if (_SiteIncomeDQO == null)
                    _SiteIncomeDQO = ObjectCreator.Create<SiteIncomeDQO>();

                return _SiteIncomeDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddSiteIncome(SiteIncomeDTO incomeDTO, DataState state)
        {
            SiteIncome income = incomeDTO.ToSiteIncome();
            income.State = state.ToString();

            income.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            incomeDTO.ID = income.ID;
        }

        public virtual void UpdateSiteIncome(SiteIncomeDTO incomeDTO, DataState state)
        {
            SiteIncome cost = SiteIncomeDQO.GetSiteIncome(incomeDTO.ID);

            if (cost.State == DataState.Publish.ToString())
                throw new BusinessException("对不起，数据已经提交不能修改");

            cost.State = state.ToString();

            incomeDTO.CopyToSiteIncome(cost);

            cost.Update();


            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual void DeleteSiteIncome(string id)
        {
            SiteIncome supplierContract = SiteIncomeDQO.GetSiteIncome(id);

            supplierContract.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual SiteIncomeDTO GetSiteIncomeDTO(string id)
        {
            SiteIncome supplierContract = SiteIncomeDQO.GetSiteIncome(id);

            return new SiteIncomeDTO(supplierContract);
        }

        #endregion
    }
}
